using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

public class InventoryManager : MonoSingleton<InventoryManager>
{
    private Dictionary<ItemData, InventoryItem> itemDic;
    public List<InventoryItem> itemList;

    public override void OnSingletonInit()
    {
        itemDic = new Dictionary<ItemData, InventoryItem>();
        itemList = new List<InventoryItem>();
    }
    
    public void AddItem(ItemData itemData)
    {
        if (itemDic.TryGetValue(itemData, out var value))
        {
            value.AddAmount();
        }
        else
        {
            var inventoryItem = new InventoryItem(itemData);
            itemDic.Add(itemData, inventoryItem);
            itemList.Add(inventoryItem);
        }
    }
    
    public void RemoveItem(ItemData itemData)
    {
        if (itemDic.TryGetValue(itemData, out var value))
        {
            if (value.GetAmount()>1)
            {
                value.RemoveAmount();
            }else
            {
                itemDic.Remove(itemData);
                itemList.Remove(value);
            }
        }
    }

    public InventoryItem GetInventoryItem (ItemData itemData)
    {
        if (itemDic.TryGetValue(itemData, out var value))
        {
            return value;
        }
        return null;
    }
    
    public void Clear()
    {
        itemDic.Clear();
        itemList.Clear();
    }

    void Start()
    {
        
    }
    void Update()
    {
        
    }
}
